import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GameFrame extends JFrame {
    //设置窗体大小
    final int frameSize = 600;
    //设置网格大小
    final int step = 30;
    int delay = 500;
    public static int score = 0;
    public static int state = 0;
    Image hqq = null;

    //蛇头对象
    SnakeHead snakeHead = new SnakeHead(GameUtils.rightImg, 60, 570, this);
    //蛇身对象
    List<SnakeBody> snakeBodyList = new ArrayList<>();
    //随机数对象
    Random random = new Random();
    public FoodObj foodObj = getRandomFood();

    public FoodObj getRandomFood(){
        int lineNum = frameSize / step;
        int x = random.nextInt(lineNum) * step;//横坐标0-570
        int y = (random.nextInt(lineNum-1)+1) * step;//纵坐标从30-570
        return new FoodObj(GameUtils.foodImg,x,y,this);
    }

    public void  launch() {
        //设置窗口是否可见
        this.setVisible(true);
        //设置窗口是否可见
        this.setSize(frameSize + 200,frameSize);
        //设置窗口的位置在屏幕上居中
        this.setLocationRelativeTo(null);
        //设置窗口的名字
        this.setTitle("贪吃蛇");
        //设置窗口关闭策略 JFrame.EXIT_ON_CLOSE：窗口关闭则程序退出
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //蛇身初始化
        snakeBodyList.add(new SnakeBody(GameUtils.bodyImg,30,570,this));
        snakeBodyList.add(new SnakeBody(GameUtils.bodyImg,0,570,this));

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_SPACE){
                    switch (state){
                        case 0:
                        case 2:
                            state = 1;break;
                        case 1:
                            state = 2;break;
                        default:break;
                    }
                }
            }
        });

        while (true){
            if(state == 1) {
                repaint();
            }//重画画板
            try {
                Thread.sleep(delay);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
    public void paintPrompt(Graphics graphics) {
        if (state == 0 || state == 2) {
            graphics.setColor(Color.red);
            graphics.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(graphics, "按空格键开始游戏", Color.white, 30, 150, 290);
        }
        if (state == 4) {
            graphics.setColor(Color.red);
            graphics.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(graphics, "恭喜你", Color.white, 30, 150, 290);
        }
        if (state == 3) {
            graphics.setColor(Color.red);
            graphics.fillRect(120, 240, 400, 70);
            GameUtils.drawWord(graphics, "小菜鸟", Color.white, 30, 150, 290);
        }
    }
    @Override
    public void paint(Graphics graphics){
        if(hqq == null){
            hqq = this.createImage(frameSize + 200,frameSize);
        }
        Graphics oldGraphics = graphics;
        graphics = hqq.getGraphics();
        //灰色背景
        graphics.setColor(Color.black);
        graphics.fillRect(0,0,frameSize + 200,frameSize);
        //设置背景
        graphics.setColor(Color.white);
        int lineNum = frameSize / step;
        for (int i =0; i <= lineNum; i++){
            //绘制横线
            graphics.drawLine(0,i*step,frameSize, i*step);
            //绘制竖线
            graphics.drawLine(i*step,0, i*step, frameSize);
        }
        //蛇身绘制
        for (int i = snakeBodyList.size() - 1; i >= 0; i--) {
            snakeBodyList.get(i).paintSelf(graphics);
        }
        //在窗口中绘制蛇头
        snakeHead.paintSelf(graphics);
        //绘制食物
        foodObj.paintSelf(graphics);
        GameUtils.drawWord(graphics,score+"分",Color.white,50,650,300);
        paintPrompt(graphics);
        oldGraphics.drawImage(hqq,0,0,null);
    }

    public static void main(String[] args) {
        GameFrame gameFrame = new GameFrame();
        gameFrame.launch();
    }
}
